package com.kaifamiao.dao.impl;

import com.kaifamiao.dao.IUserInfoMapper;
import com.kaifamiao.model.QueryVo;
import com.kaifamiao.model.UserInfoModel;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserInfoMapperImpl implements IUserInfoMapper {

    private SqlSessionFactory factory;

    public UserInfoMapperImpl(SqlSessionFactory factory) {
        this.factory = factory;
    }

    @Override
    public List<UserInfoModel> findAll() {
        SqlSession session = factory.openSession();
        List<UserInfoModel> list = session.selectList("com.kaifamiao.dao.IUserInfoMapper.findAll");
        session.close();
        return list;
    }

    @Override
    public List<UserInfoModel> findAll2(UserInfoModel userInfoModel) {
        return null;
    }

    @Override
    public UserInfoModel findById(String id) {
        SqlSession session = factory.openSession();
        UserInfoModel userInfoModel = session.selectOne("com.kaifamiao.dao.IUserInfoMapper.findById", id);
        session.close();
        return userInfoModel;
    }

    @Override
    public List<UserInfoModel> findByUser(UserInfoModel userInfoModel) {
        return null;
    }

    @Override
    public int saveUser(UserInfoModel userInfoModel) {
        SqlSession session = factory.openSession();
        int rows = session.update("com.kaifamiao.dao.IUserInfoMapper.saveUser", userInfoModel);
        session.commit();
        session.close();
        return rows;
    }

    /**
     * 传进来的 id 可以为 null
     * @param userInfoModel
     * @return
     */
    @Override
    public int saveWithUuid(UserInfoModel userInfoModel) {
        SqlSession session = factory.openSession();
        int rows = session.update("com.kaifamiao.dao.IUserInfoMapper.saveWithUuid", userInfoModel);
        session.commit();
        session.close();
        return rows;
    }

    @Override
    public int updateById(UserInfoModel userInfoModel) {
        SqlSession session = factory.openSession();
        int rows = session.update("com.kaifamiao.dao.IUserInfoMapper.updateById", userInfoModel);
        session.commit();
        session.close();
        return rows;
    }

    @Override
    public int deleteById(String id) {
        SqlSession session = factory.openSession();
        int rows = session.update("com.kaifamiao.dao.IUserInfoMapper.deleteById", id);
        session.commit();
        session.close();
        return rows;
    }

    @Override
    public int deleteByIdLogic(String id) {
        SqlSession session = factory.openSession();
        int rows = session.update("com.kaifamiao.dao.IUserInfoMapper.deleteByIdLogic", id);
        session.commit();
        session.close();
        return rows;
    }

    @Override
    public List<UserInfoModel> findByUserName(String userName) {
        SqlSession session = factory.openSession();
        List<UserInfoModel> list = session.selectList("com.kaifamiao.dao.IUserInfoMapper.findByUserName", userName);
        session.close();
        return list;
    }

    @Override
    public int findTotal() {
        SqlSession session = factory.openSession();
        int total = session.selectOne("com.kaifamiao.dao.IUserInfoMapper.findTotal");
        session.close();
        return total;
    }

    @Override
    public UserInfoModel findByVo(QueryVo queryVo) {
        SqlSession session = factory.openSession();
        UserInfoModel userInfoModel = session.selectOne("com.kaifamiao.dao.IUserInfoMapper.findByVo", queryVo);
        session.close();
        return userInfoModel;
    }

    @Override
    public int deleteByIds(String... ids) {
        return 0;
    }
}
